package edu.stanford.hardon.Tabs;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class TabHandler extends ImageView
{
	private Paint					mPaint;
    private Paint					mActiveTextPaint;
    private Paint					mInactiveTextPaint;
    private ArrayList<GenericTab>	mTabMembers;
    private int						mActiveTab;
    private OnTabClickListener		mOnTabClickListener = null;
    private static final String TAG = "TabHandler";
	public TabHandler( Context context, AttributeSet attrs ) 
	{
		super(context, attrs);
		
		mTabMembers = new ArrayList<GenericTab>( );
		
		mPaint = new Paint( );
		mActiveTextPaint = new Paint( );
		mInactiveTextPaint = new Paint( );
		
		mPaint.setStyle( Paint.Style.FILL );
		mPaint.setColor( 0xFFFFFF00 );
		
		mActiveTextPaint.setTextAlign( Align.CENTER );
		mActiveTextPaint.setTextSize( 12 );
		mActiveTextPaint.setColor( 0xFFFFFFFF );
		mActiveTextPaint.setFakeBoldText( true );
		
		mInactiveTextPaint.setTextAlign( Align.CENTER );
		mInactiveTextPaint.setTextSize( 12 );
		mInactiveTextPaint.setColor( 0xFF999999 );
		mInactiveTextPaint.setFakeBoldText( true );
		
		mActiveTab = 0;
		
		this.setOnTabClickListener( new TabHandler.OnTabClickListener( ) {
        	public void onTabClick( int tabId )
        	{
        		for(int i = 0; i < mTabMembers.size(); i++){
        			GenericTab gt = mTabMembers.get(i);
        			if(i != (tabId-1)){
            			gt.hideTab();
        			}
        		}
        		for(int i = 0; i < mTabMembers.size(); i++){
        			GenericTab gt = mTabMembers.get(i);
        			if(i == (tabId-1)){
        				gt.showTab();
        			}
        		}
        		
        		setActiveTab(tabId);
        		
        	}
        });
		
	}
	
	public void setActiveTab(int tabNum){
		mActiveTab = tabNum-1;
	}
	
	public int getActiveTabNum(){
		return mActiveTab+1;
	}
	
	public GenericTab getActiveTab(){
		return mTabMembers.get(mActiveTab);
	}
	
	/**
	 * pauseCurrent - pauses the current tab
	 */
	public void pauseCurrent(){
		GenericTab currentTab = mTabMembers.get(mActiveTab);
		currentTab.setPaused();
	}
	
	public void resumeCurrent(){
		GenericTab currentTab = mTabMembers.get(mActiveTab);
		currentTab.setResumed();
	}
	
    protected void onDraw( Canvas canvas )
    {
    	Log.d( TAG, "iTab onDraw start" );
    	super.onDraw( canvas );
    	
    	Rect r = new Rect( );
    	this.getDrawingRect( r );
    	
    	// Calculate how many pixels each tab can use
    	int singleTabWidth = r.right / ( mTabMembers.size( ) != 0 ? mTabMembers.size( ) : 1 );
    	
    	Log.d( TAG, "iTab: SingleTabWidth: " + singleTabWidth );
    	
    	// Draw the background
    	canvas.drawColor( 0xFF000000 );
    	mPaint.setColor( 0xFF434343 );
    	canvas.drawLine( r.left, r.top + 1, r.right, r.top + 1, mPaint );
    	
    	int color = 46;
    	
    	for( int i = 0; i < 24; i++ )
    	{
    		mPaint.setARGB( 255, color, color, color );
    		canvas.drawRect( r.left, r.top + i + 1, r.right, r.top + i + 2, mPaint );
    		color--;
    	}

    	// Draw each tab
    	for( int i = 0; i < mTabMembers.size( ); i++ )
    	{
    		GenericTab tabMember = mTabMembers.get( i );
    		
    		Bitmap icon = BitmapFactory.decodeResource( getResources( ), tabMember.getIconResourceId( ) );
			Bitmap iconColored = Bitmap.createBitmap( icon.getWidth(), icon.getHeight(), Bitmap.Config.ARGB_8888 );
			Paint p = new Paint( Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
			Canvas iconCanvas = new Canvas( );
			iconCanvas.setBitmap( iconColored );

			if( mActiveTab == i )
			{
				p.setShader( new LinearGradient( 0, 0, icon.getWidth(), icon.getHeight(), 0xFFFFFFFF, 0xFF54C7E1, Shader.TileMode.CLAMP ) );
			}
			else {    
				p.setShader( new LinearGradient( 0, 0, icon.getWidth(), icon.getHeight(), 0xFFA2A2A2, 0xFF5F5F5F, Shader.TileMode.CLAMP ) );
			}
			
			iconCanvas.drawRect( 0, 0, icon.getWidth( ), icon.getHeight( ), p );
			
			for( int x = 0; x < icon.getWidth(); x++ )
			{
				for( int y = 0; y < icon.getHeight(); y++ )
				{
					if( ( icon.getPixel(x, y) & 0xFF000000 ) == 0 )
					{
						iconColored.setPixel( x, y, 0x00000000 );
					}
				}
			}
			
    		// Calculate Tab Image Position
    		int tabImgX = singleTabWidth * i + ( singleTabWidth / 2 - icon.getWidth( ) / 2 );
    		
    		// Active Tab will be drawn in a different way
    		if( mActiveTab == i )
    		{		
    			mPaint.setARGB( 37, 255, 255, 255 );
    			canvas.drawRoundRect(  new RectF( r.left + singleTabWidth * i + 3, r.top + 3, r.left + singleTabWidth * ( i + 1 ) - 3, r.bottom - 2 ), 5, 5, mPaint );
    			canvas.drawBitmap( iconColored, tabImgX , r.top + 5, null );
    			canvas.drawText( tabMember.getText( ), singleTabWidth * i + ( singleTabWidth / 2), r.bottom - 2, mActiveTextPaint );
    		} else
    		{
    			canvas.drawBitmap( iconColored, tabImgX , r.top + 5, null );
    			canvas.drawText( tabMember.getText( ), singleTabWidth * i + ( singleTabWidth / 2), r.bottom - 2, mInactiveTextPaint );
    		}
    	}

    	Log.d( TAG, "iTab onDraw finished" );
    }
    
    public boolean onTouchEvent( MotionEvent motionEvent )
    {
    	Rect r = new Rect( );
    	this.getDrawingRect( r );        	
    	float singleTabWidth = r.right / ( mTabMembers.size( ) != 0 ? mTabMembers.size( ) : 1 );
    	
    	int pressedTab = (int) ( ( motionEvent.getX( ) / singleTabWidth ) - ( motionEvent.getX( ) / singleTabWidth ) % 1 );
    	
    	mActiveTab = pressedTab;
    	
    	if( this.mOnTabClickListener != null)
    	{
    		this.mOnTabClickListener.onTabClick( mTabMembers.get( pressedTab ).getId( ) );        	
    	}
    	
    	this.invalidate();
    	
    	return super.onTouchEvent( motionEvent );
    }
    
    public void addTabMember( GenericTab tabMember )
    {
    	mTabMembers.add( tabMember );
    }
    
    public void setOnTabClickListener( OnTabClickListener onTabClickListener )
    {
    	mOnTabClickListener = onTabClickListener;
    }
    
    
    public static interface OnTabClickListener
    {
    	public abstract void onTabClick( int tabId );
    }
}
